package controller

import (
	"encoding/json"
	"github.com/shirou/gopsutil/v4/disk"
	"github.com/shirou/gopsutil/v4/mem"
	"lktx-dns/bean"
	"lktx-dns/stat"
	"net/http"
	"strconv"
)

type StatisticsInfo struct {
}

func (receiver StatisticsInfo) StatisticsHandler(w http.ResponseWriter, r *http.Request) {
	queryParams := r.URL.Query()
	typeStr := queryParams.Get("type")
	num, _ := strconv.Atoi(typeStr)
	json.NewEncoder(w).Encode(bean.ResOk(stat.StatData.GetData(num)))
	return
}

func (receiver StatisticsInfo) StatisticsGroupHandler(w http.ResponseWriter, r *http.Request) {
	json.NewEncoder(w).Encode(bean.ResOk(stat.StatData.GetGroupType()))
	return
}

func (receiver StatisticsInfo) StatisticsTypeTimeHandler(w http.ResponseWriter, r *http.Request) {
	queryParams := r.URL.Query()
	time := queryParams.Get("time")
	typeStr := queryParams.Get("type")
	num, _ := strconv.Atoi(typeStr)
	json.NewEncoder(w).Encode(bean.ResOk(stat.StatData.GetTimeType(time, num)))
	return
}

func (receiver StatisticsInfo) SystemInfoHandler(w http.ResponseWriter, r *http.Request) {
	v, _ := mem.VirtualMemory()
	d, _ := disk.Usage("/")
	data := map[string]interface{}{}
	data["mem"] = v
	data["disk"] = d
	json.NewEncoder(w).Encode(bean.ResOk(data))
	return
}
